<template>
  <div class="register">
    <div class="card">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="122px"
        class="demo-ruleForm myfrom" run>
        <h1>欢迎加入智能家政服务平台</h1>
        <h5>家政公司账号注册</h5>
     
        <el-form-item  label="家政公司账号名：" prop="homeaccount"> 
        <label slot="label" class="homeReLabel">家政公司账号名：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label> 
          <el-input type="text" clearable v-model="ruleForm.account" autocomplete="off"></el-input>
       </el-form-item>
       
        <el-form-item  label="法人代表：" prop="username"> 
            <label slot="label" >法&nbsp;&nbsp;人&nbsp;&nbsp;代&nbsp;&nbsp;表：</label>  
          <el-input type="text" clearable v-model="ruleForm.username" autocomplete="off"></el-input>
       </el-form-item>
        <el-form-item  label="密码：" prop="password">
          <label slot="label">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:</label>
          <el-input type="password" show-password v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="确认密码：" prop="checkPass" >
            <label slot="label" >确&nbsp;&nbsp;认&nbsp;&nbsp;密&nbsp;&nbsp;码：</label> 
          <el-input type="password" show-password v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
      
       <el-form-item label=" 法人电话：" >
             <label slot="label" >法&nbsp;&nbsp;人&nbsp;&nbsp;电&nbsp;&nbsp;话：</label> 
          <el-input type="tel"  v-model="ruleForm.tel" ></el-input>
        </el-form-item>  
         <el-form-item label=" 验证码:" prop="code">
           <label slot="label">验&nbsp;&nbsp;&nbsp;&nbsp;证&nbsp;&nbsp;&nbsp;&nbsp;码:</label>
          <el-input clearable v-model="ruleForm.code" maxlength="5">
            <template #append>
              <el-button @click="getCaptcha">刷新验证码</el-button>
            </template>
          </el-input>
        </el-form-item>
        
        <el-form-item class="code_img" v-show="showElImage" >
          <el-image
            style=" width: 158px;height: 42px ;margin-left:60px"
            :src="captchaImg"
            class="captchaImg"
            @click="getCaptcha"
          ></el-image>
        </el-form-item>
        <!-- <el-form-item label="验证码:" prop="code">
       <el-input class="code_input" v-model="ruleForm.code "></el-input>
       <el-input class="code"  >12345</el-input>
        </el-form-item> -->
        <el-form-item  label="公司地址：">  
         <label slot="label">公&nbsp;&nbsp;司&nbsp;&nbsp;地&nbsp;&nbsp;址:</label>
          <el-input type="text" v-model="ruleForm.address"></el-input>
       </el-form-item>
        <el-form-item>
          <el-button class="mar_del" type="primary" @click="submitForm('ruleForm')">注册</el-button>
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
          <!-- <el-button class="mar_del_back" @click="resetForm('ruleForm')">返回</el-button>  -->
           <el-button class="mar_del_back" @click="backForm()">返回</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <router-link
            to="/home"
            class="btn btn-outline-primary"
            style="margin-right:5px;"
            tag="button"
          >回到首页</router-link>
          <router-link to="/login" class="btn btn-outline-primary" tag="button">已有账号</router-link>
        </el-form-item> -->
     
      </el-form>

    </div>
  </div>
</template>

<script>
import qs from "qs";
export default {
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      captchaImg:"",
      token:"",
      ruleForm: {
        homeaccount:"",
        username: "",
        password: "",
        checkPass: "",
        sex:"",
        tel:"",
        code:"",
        address:"",
      },
      rules: {
          homeaccount:[
             { required: true, message: "请输入家政公司账号名", trigger: "blur" },
          
          {
             min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        username: [
          { required: true, message: "请输入法人代表", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
          code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const data = await this.$http.post(
            // "/sys/user/register?token="+this.token+"&code="+this.ruleForm.code,
           {
                username: this.ruleForm.username,
                password: this.ruleForm.password,
                sex:this.ruleForm.sex,
                phone:this.ruleForm.phone,
                address:this.ruleForm.address,
               
              }
          );

          if (data.code != 200) {
            this.$refs[formName].resetFields();
            this.$message.error("注册失败！");
          } else {
            this.$refs[formName].resetFields();
            this.$message.success("注册成功！");

            this.$router.push("/login");
          }
        }
      });
    },
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // },
    backForm() {
       this.$router.push("/login");
    },

    suer() {
      if (this.ruleForm.checkPass != this.ruleForm.password) {
        this.$message.error("两次密码不同！！");
      }
    },
     getCaptcha() {
      this.$http.get('/captcha').then((res) => {
        this.token = res.data.token
        this.captchaImg = res.data.captchaImg
        this.ruleForm.code = ''
      })
     }
  },
  created() {
    this.getCaptcha()
  },
};
</script>

<style lang="less" scoped>
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100vw;
  height: 120vh;

  //  background: url("~@/assets/imags/login/login.jpg") center center no-repeat;
     background: url("~@/assets/imags/login/login.jpeg") no-repeat  ; 
     background-size: cover;
}
.card {
  width: 28rem;
  padding: 20px;
  background: rgba(255, 255, 255, 0.8);
  border: 0.5px solid rgba(127, 255, 212, 0.3);
}
.el-form {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}
.el-message {
  top: 200px !important;
}
h1 {
  // letter-spacing: 6px;
  font-size: 18px;
  margin-top: 8px;
  color: #888888;
}
h2 {
  margin-top: 5px;
}
.btn-outline-primary {
  color: #fff !important;
}
.el-input--suffix {
  width: 220px !important;
}
.el-form-item {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  margin-bottom: 18px;
}
.sexSelect{
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  margin-left: 20px;
}
.el-form-item__content {
  display: flex !important;
}
.sex_style {
  margin-right: 130px !important;
  // margin-bottom: 22px;
}
.sex_radio{
  position: relative;
  right: 106px;
}
.radio__label {
    font-size: 17px;
    padding-left: 10px;
}

</style>
